<template>
	<view class="page-container">
		<!-- Tab 切换 -->

		<div class="header_nav">
			<div class="header_nav_item"
				:style="activeTab == item.value? 'color:#ffffff; font-weight:bold;':'color:#fff;'"
				v-for="(item, index) of navTab" :key="index" @click="switchTab(item.value)">
				{{item.label}}
				<span class="nav_item_bottom" v-show="activeTab == item.value"></span>
			</div>
		</div>
		<!-- <view class="tab-container">
		<view 
		  :class="['tab-item', activeTab === 'all' ? 'active' : '']" 
		  @click="switchTab('all')">全部</view>
		<view 
		  :class="['tab-item', activeTab === 'chuangye' ? 'active' : '']" 
		  @click="switchTab('chuangye')">创业板</view>
		<view 
		  :class="['tab-item', activeTab === 'zhu' ? 'active' : '']" 
		  @click="switchTab('zhu')">主板</view>
	  </view> -->
		<!-- <view class="title">股票数据列表</view> -->
		<scroll-view scroll-y class="stock-box" v-if="stockData.length > 0">

			<div class="roce_top">
				<div :class="item.class" v-for="(item, index) of roceBox" :key="index">
					<div class="ranking_hg"></div>
					<p class="roce_label">{{hotList[item.idx].name || ''}}</p>
					<p class="roce_code">{{hotList[item.idx].code || ''}}</p>
					<p class="roce_price" :style="Number(hotList[item.idx].rate) >= 0? 'color:red;':'color:#00aa7f;'">
						{{hotList[item.idx].rate == '-'?'0.00':hotList[item.idx].rate || '0.00'}}%
					</p>
					<p class="roce_price" :style="Number(hotList[item.idx].rate) >= 0? 'color:red;':'color:#00aa7f;'">
						{{hotList[item.idx].price || ''}}
					</p>
				</div>
			</div>
			<view class="stock-item" v-for="(stock, index) in stockData" :key="index">
				<view class="stock-item-name">股票名称：{{ stock.name }}</view>
				<view class="stock-item-content">
					<view class="stock-item-field stock-item-code" style="margin-top: 0;">
						股票代码：<span>{{ stock.code }}</span></view>
					<view class="stock-item-field">
						<text class="stock-item-text" style="color: red;">价格：{{ stock.price }}</text>
						<text class="stock-item-text" style="color: red;">涨跌幅：{{ stock.rate }}%</text>
					</view>
					<view class="stock-item-field stock-item-description">描述：<span>{{ stock.description }}</span></view>
				</view>
			</view>
		</scroll-view>
		<view v-else>
			<view>盘中不展示，每日收盘后执行模型运算，当天推送次日的策略标的，可联系管理员加入策略推送专群</view>
		</view>
	</view>
</template>
<script>
	import {
		requestWithToken
	} from '@/utils/request.js';
	export default {
		data() {
			return {
				stockData: [],
				activeTab: 'all', // 默认选中全部全部
				navTab: [{
						label: "全部",
						value: "all"
					},
					{
						label: "创业板",
						value: "chuangye"
					},
					{
						label: "主板",
						value: "zhu"
					},
				],
				hotList: [],
				roceBox: [{
						class: "roce_top_one roce_top_two",
						idx: 1
					},
					{
						class: "roce_top_one",
						idx: 0
					},
					{
						class: "roce_top_one roce_top_three",
						idx: 2
					},
				]
			};
		},
		onLoad() {
			this.fetchStockData(this.activeTab); // 页面加载时默认获取全部数据
		},
		methods: {
			// 切换 Tab
			switchTab(tab) {
				if (this.activeTab !== tab) {
					this.activeTab = tab;
					this.fetchStockData(tab); // 切换时重新获取数据
				}
			},
			// 获取股票数据
			async fetchStockData(type) {
				const urlMap = {
					all: 'http://123.56.82.144:8090/stock/getStockDayData', // 全部接口
					chuangye: 'http://123.56.82.144:8090/stock/getStockDayData?type=1', // 创业板接口
					zhu: 'http://123.56.82.144:8090/stock/getStockDayData?type=2', // 主板接口
				};
				try {
					const res = await requestWithToken({
						url: urlMap[type],
						method: 'GET'
					});
					if (res.statusCode === 200 && res.data && res.data.data) {
						let data = res.data.data;
						this.hotList = data.slice(0, 3);
						this.stockData = data.slice(3, data.length);
					} else {
						uni.showToast({
							title: '获取数据失败',
							icon: 'none',
						});
					}
				} catch (err) {
					uni.showToast({
						title: '请求失败',
						icon: 'none',
					});
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.page-container {
		// padding: 10px;
	}

	.title {
		font-size: 24px;
		margin-bottom: 20px;
		text-align: center;
	}

	.stock-box {
		padding: 0 20rpx;
		height: calc(100vh - 210rpx);
	}

	.stock-item {
		margin-bottom: 10px;
	}

	.stock-item-name {
		background: rgb(247, 62, 60);
		color: #fff;
		font-size: 16px;
		line-height: 16px;
		padding: 8px 10px;
		border-top-left-radius: 5px;
		border-top-right-radius: 5px;
	}

	.stock-item-content {
		font-size: 14px;
		line-height: 16px;
		padding: 8px 10px;
		border: 1px solid #ccc;
		border-top: 0;
		border-bottom-left-radius: 5px;
		border-bottom-right-radius: 5px;
	}

	.stock-item-field {
		margin: 5px 0;
	}

	.stock-item-code {
		color: #888;
	}

	.stock-item-text {
		margin-right: 20px;
	}

	.stock-item-description {
		color: #333;
		line-height: 18px;
	}

	.tab-container {
		display: flex;
		justify-content: center;
		margin-bottom: 20px;
	}

	.tab-item {
		padding: 10px 20px;
		border: 1px solid #ccc;
		cursor: pointer;
		margin: 0 5px;
		border-radius: 5px;
	}

	.tab-item.active {
		background-color: #f73e3c;
		color: white;
		border-color: #f73e3c;
	}



	.header_nav {
		display: flex;
		align-items: center;
		color: #333;
		background-color: rgb(255, 78, 34);
		justify-content: space-evenly;
		padding: 20rpx 0;
		margin-bottom: 20rpx;

		.header_nav_item {
			font-size: 32rpx;
			margin-left: 15rpx;
			position: relative;
			display: flex;
			align-items: center;
			justify-content: center;
			padding-bottom: 8rpx;
			white-space: nowrap;
			width: 70rpx;
			height: 60rpx;
			transition: all 0.2s ease-in-out;

			.nav_item_bottom {
				position: absolute;
				bottom: 0;
				width: 50rpx;
				height: 5rpx;
				background-color: #f9b25b;
				border-radius: 50rpx;
			}
		}
	}



	.roce_top {
		width: calc(100% - 40rpx);
		height: 410rpx;
		padding: 40rpx 20rpx;
		display: flex;

		.roce_top_one {
			width: 180rpx;
			height: 325rpx;
			border: 12rpx solid rgba(252, 202, 95, 1);
			border-radius: 300rpx;
			position: relative;
			margin: 0 20rpx;
			background-color: #fff;
			display: flex;
			flex-flow: column;
			align-items: center;
			justify-content: center;
		}

		.roce_top_two {
			border-color: rgba(219, 219, 219, 1);
			margin-top: 40rpx;

			.ranking_hg {
				background-position: -80rpx;
			}
		}

		.roce_top_three {
			border-color: rgba(255, 169, 134, 1);
			margin-top: 80rpx;

			.ranking_hg {
				background-position: -160rpx;
			}
		}

		.roce_label {
			font-size: 32rpx;
			font-weight: bold;
		}

		.roce_code {
			font-size: 26rpx;
			margin-top: 12rpx;
			color: #999;
		}

		.roce_price {
			margin-top: 12rpx;
			font-size: 32rpx;
		}
	}


	.ranking_hg {
		width: 80rpx;
		height: 80rpx;
		background: url('@/static/icon/ranking_hg.png') no-repeat;
		background-size: 240rpx;
		position: absolute;
		top: -35rpx;
		left: -40rpx;
	}
</style>